<?php


namespace app\common\model;


use think\Model;

class AuthRole extends Model
{
    protected $createTime = 'add_time';
    protected $updateTime = 'up_time';

    public $autoWriteTimestamp = true;

    protected $insert = [
        'status' => 1,//1-启用，0-禁用
    ];



    public function create_data($data)
    {
        return $this->allowField(true)->save($data);
    }

    public function getAll($condition = [],$field='id,name')
    {
        $condition[]=['status','=',1];
        return self::where($condition)
            ->order('sort,id desc')->field($field)
            ->select();

    }
    public function list_data($condition = [], $page = 1, $pageSize = 10)
    {
        return self::where(function ($query) use ($condition) {
            if (!empty($condition['keyword'])) $query->where('name', 'like', '%' . $condition['keyword'] . '%');
        })
            ->limit(($page - 1) * $pageSize, $pageSize)
            ->order('id desc')
            ->paginate($pageSize, false, ['query' => request()->param()]);

    }

    public function list_data_for_check($condition, $limit = 30)
    {
        return self::where($condition)
            ->order('id asc')
            ->field(true)
            ->limit(0, $limit)
            ->select()
            ->toArray();
    }

    public function update_data($data)
    {
        return $this->allowField(true)->isUpdate(true)->save($data);
    }

    public function fetch_data($id)
    {
        $data = self::get($id);
        return $data ? $data->toArray() : [];
    }

    public function del_data($id)
    {
        return $this->where(['id'=>$id])->delete();
    }

}